/*
 * EmbeddedAgentBean.java is part of htn-core.
 *
 * EmbeddedAgentBean.java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * EmbeddedAgentBean.java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EmbeddedAgentBean.java.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.htn;

import org.htn.authentication.AuthenticationException;
import org.htn.authentication.Authenticator;
import org.htn.repository.DocumentManager;
import org.htn.repository.DocumentManagerException;
import org.htn.rtml.RTMLError;
import org.htn.rtml.RTMLRequest;
import org.htn.validation.ValidationException;
import org.htn.validation.Validator;

/**
 * Main orchestrator.
 * 
 * Performs the following operations on the RTML document, in the order below:
 *   - validation (if configured)
 *   - authentication (if configured)
 *   - persistence (if configured)
 *   - forwarding (must be configured)
 * 
 * @author pwarner
 *
 */
public class EARequestBean implements EARequest {

	private Validator<String> validator;
	private Authenticator<String> authenticator;
	private DocumentManager<String> documentManager;
	private RTMLRequest<String> rtmlRequest;

	private RTMLError rtmlError;

	/**
	 * Receive the RTML document (as a String).
	 * 
	 * This implementation validates (if configured), 
	 */
	public String receive(final String rtml) {
		String rtmlProc = null;
		try {
			validate(rtml);
			authenticate(rtml);
			rtmlProc = persist(rtml);
			rtmlProc = sendRTML(rtmlProc);
			validate(rtmlProc);
		} catch (ValidationException e) {
			rtmlProc = rtmlError.buildError(rtml,e);
		} catch (AuthenticationException e) {
			rtmlProc = rtmlError.buildError(rtml,e);
		} catch (DocumentManagerException e) {
			rtmlProc = rtmlError.buildError(rtml,e);
		}
		return rtmlProc;
	}

	private String sendRTML(String rtml) {
		rtml = this.rtmlRequest.send(rtml);
		return rtml;
	}

	private void authenticate(String rtml) throws AuthenticationException {
		if (null != this.authenticator) {
			this.authenticator.authenticate(rtml);
		}
	}

	private String persist(String rtml) throws DocumentManagerException {
		if (null != this.documentManager) {
			rtml = this.documentManager.persist(rtml);
		}
		return rtml;
	}
	
	private void validate(String rtml) throws ValidationException {
		if (null != this.validator) {
			this.validator.validate(rtml);
		}
	}

	public void setAuthenticator(Authenticator<String> authenticator) {
		this.authenticator = authenticator;
	}

	public void setDocumentManager(DocumentManager<String> documentManager) {
		this.documentManager = documentManager;
	}

	public void setRtmlError(RTMLError rtmlError) {
		this.rtmlError = rtmlError;
	}

	public void setRtmlRequest(RTMLRequest<String> rtmlRequest) {
		this.rtmlRequest = rtmlRequest;
	}

	public void setValidator(Validator<String> validator) {
		this.validator = validator;
	}

}

